"use strict";
var __extends = (this && this.__extends) || (function () {
    var extendStatics = function (d, b) {
        extendStatics = Object.setPrototypeOf ||
            ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
            function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
        return extendStatics(d, b);
    };
    return function (d, b) {
        extendStatics(d, b);
        function __() { this.constructor = d; }
        d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
    };
})();
Object.defineProperty(exports, "__esModule", { value: true });
var UIComponent_1 = require("../basic/models/UIComponent");
var UIIcon_1 = require("./UIIcon");
var ElementHelper = require("../utils/ElementHelper");
var CastHelper = require("../utils/CastHelper");
var TypeHelper = require("../utils/TypeHelper");
var ExceptionHelper = require("../utils/ExceptionHelper");
var UITabItem = (function (_super) {
    __extends(UITabItem, _super);
    function UITabItem(app, options) {
        var _this = _super.call(this, app) || this;
        _this.icon = "tab";
        _this.useSubTitle = false;
        _this.closable = true;
        _this.height = 37;
        _this.minWidth = 40;
        _this.maxWidth = 180;
        _this._content = null;
        _this._element = null;
        if (!TypeHelper.isStringWithNotEmpty(options.id))
            ExceptionHelper.assertId();
        _this.id = options.id;
        if (!TypeHelper.isStringWithNotEmpty(options.title))
            ExceptionHelper.assertTitle();
        _this.title = options.title;
        if (!TypeHelper.isStringWithNotEmpty(options.url))
            ExceptionHelper.assertUrl();
        _this.url = options.url;
        _this.elementId = _this.app.prefix + "tab-item" + "-" + _this.id;
        _this.icon = CastHelper.typeOrBooleanCast(options.icon, _this.icon);
        _this.useSubTitle = CastHelper.booleanCast(options.useSubTitle, _this.useSubTitle);
        _this.closable = CastHelper.booleanCast(options.closable, _this.closable);
        _this.height = Math.min(CastHelper.numberCast(options.height, _this.height), _this.height);
        _this.maxWidth = Math.min(CastHelper.numberCast(options.maxWidth, _this.maxWidth), _this.maxWidth);
        _this.minWidth = Math.max(CastHelper.numberCast(options.minWidth, _this.minWidth), _this.minWidth);
        return _this;
    }
    Object.defineProperty(UITabItem.prototype, "content", {
        get: function () {
            return this._content;
        },
        set: function (value) {
            this._content = value;
        },
        enumerable: true,
        configurable: true
    });
    Object.defineProperty(UITabItem.prototype, "element", {
        get: function () {
            return document.getElementById(this.elementId);
        },
        enumerable: true,
        configurable: true
    });
    UITabItem.prototype.present = function () {
        var _this = this;
        var fragment = ElementHelper.createFragment();
        var tabItemElement = ElementHelper.createElement("div");
        tabItemElement.id = this.elementId;
        tabItemElement.setAttribute("data-tab-id", this.id);
        ElementHelper.addClasses(tabItemElement, this.app.prefix, "tab-item", "flexbox", "flex-row", "flex-vertical-center");
        ElementHelper.addStyles(tabItemElement, {
            height: this.height + "px",
            maxWidth: this.maxWidth + "px",
            minWidth: this.minWidth + "px"
        });
        tabItemElement.addEventListener("mousedown", function (ev) {
            _this.switch();
        }, true);
        this.createIcon(tabItemElement);
        this.createLabel(tabItemElement);
        this.createCloseIcon(tabItemElement);
        fragment.appendChild(tabItemElement);
        return fragment;
    };
    UITabItem.prototype.createIcon = function (tabItemElement) {
        if (this.icon === false)
            return;
        var tabItemIconElement = ElementHelper.createElement("div");
        ElementHelper.addClasses(tabItemIconElement, this.app.prefix, "tab-item" + "-icon", "flexbox", "flex-center");
        var icon = new UIIcon_1.default(this.app, this.icon);
        var iconElement = icon.present();
        tabItemIconElement.appendChild(iconElement);
        tabItemElement.appendChild(tabItemIconElement);
    };
    UITabItem.prototype.createLabel = function (tabItemElement) {
        var tabItemLabelElement = ElementHelper.createElement("div");
        ElementHelper.addClasses(tabItemLabelElement, this.app.prefix, "tab-item" + "-label");
        tabItemLabelElement.innerText = this.title;
        tabItemElement.appendChild(tabItemLabelElement);
    };
    UITabItem.prototype.createCloseIcon = function (tabItemElement) {
        var _this = this;
        if (this.closable === false)
            return;
        var tabItemCloseElement = ElementHelper.createElement("div");
        ElementHelper.addClasses(tabItemCloseElement, this.app.prefix, "tab-item" + "-close", "flexbox", "flex-center");
        tabItemCloseElement.addEventListener("mousedown", function (ev) {
            ElementHelper.removeElement(_this.element);
            ElementHelper.removeElement(_this.content.element);
            var index = _this.app.items.indexOf(_this);
            _this.app.items.splice(index, 1);
            _this.app.item = null;
            if (_this.app.lastItem)
                _this.app.lastItem.switch();
            else {
                var nextItem = _this.app.items[_this.app.items.length - 1];
                if (nextItem)
                    nextItem.switch();
            }
        }, true);
        var icon = new UIIcon_1.default(this.app, "destroy");
        var iconElement = icon.present();
        tabItemCloseElement.appendChild(iconElement);
        tabItemElement.appendChild(tabItemCloseElement);
    };
    UITabItem.prototype.switch = function () {
        if (this === this.app.item)
            return;
        this.app.lastItem = this.app.item;
        this.app.item = this;
        if (this.app.lastItem && this.app.lastItem.content && this.app.lastItem.content.element) {
            ElementHelper.removeClasses(this.app.lastItem.element, this.app.prefix, "tab-item" + "-active");
            ElementHelper.removeClasses(this.app.lastItem.content.element, this.app.prefix, "tab-content" + "-active");
        }
        ElementHelper.addClasses(this.element, this.app.prefix, "tab-item" + "-active");
        ElementHelper.addClasses(this.content.element, this.app.prefix, "tab-content" + "-active");
        this.updateOffset();
    };
    UITabItem.prototype.updateOffset = function () {
        var left = 0;
        if (this.app.items && this.app.items.length > 0) {
            var itemElemnet = this.app.items[this.app.items.length - 1].element;
            left = itemElemnet.offsetLeft + itemElemnet.offsetWidth;
        }
        return left;
    };
    UITabItem.prototype.updateTitle = function (title) {
        var element = this.element;
        if (!element)
            return;
        var titleElement = element.querySelector("." + this.app.prefix + "tab-item-label");
        if (!titleElement)
            return;
        titleElement.innerText = title;
        titleElement.setAttribute("title", title);
        this.title = title;
    };
    return UITabItem;
}(UIComponent_1.default));
exports.default = UITabItem;
